# coding:utf-8

import sys
import os
import time
import unittest
#import HTMLReport

#from template.HTMLTestRunner1 import HTMLTestRunner
#from template.HTMLTestRunner  import HTMLTestRunner
from template.HTMLTestRunner_Charts import HTMLTestRunner

from services.gen_test_case import testcase_generator

currentUrl = os.path.dirname(__file__)
parentUrl = os.path.abspath(os.path.join(currentUrl, os.pardir))
sys.path.append(parentUrl)


def run(file_name):
    # 设置报告文件保存路径
    # report_path = os.path.dirname(os.path.abspath('.')) + '/test_report/'
    report_path = parentUrl + '/report/'
    # 获取系统当前时间
    now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))

    # 设置报告名称格式
    # html__report_name = report_path + "Api Test Report_" + now + ".html"
    html__report_name = report_path + "Api Test Report.html"
    fp = open(html__report_name, "w", encoding='utf-8')

    suites = []
    for case in testcase_generator(file_name):
        suites.append(unittest.TestLoader().loadTestsFromTestCase(case))
    # 构建suite
    alltests = unittest.TestSuite(suites)

    # runner = unittest.TextTestRunner(verbosity=2)
    # runner.run(alltests)

    runner = HTMLTestRunner(stream=fp, title=u"COO项目测试报告", description=u"用例测试情况")

    # 开始执行测试套件
    runner.run(alltests)
